﻿namespace Common.ChartDrawing
{
    using System.Windows;

    public static class ChartScaler
    {
        private static CanvasElements elements = null;

        public static void ScaleElements(double coef, CanvasElements elements)
        {
            ChartScaler.elements = elements;

            Constants.MillisecondsByPixel *= coef;
            Constants.StartCharts *= coef;
            Constants.EndCharts *= coef;

            ChartScaler.ScaleCharts(coef);
            ChartScaler.ScaleGrid(coef);
            ChartScaler.ScaleDateTimeLabels(coef);
            ChartScaler.ScaleRedLine(coef);

            ChartScaler.elements = null;
        }

        private static void ScaleGrid(double coef)
        {
            // grid horizontals
            if (ChartScaler.elements.GridHorizontals != null)
            {
                foreach (var gridHorizontal in ChartScaler.elements.GridHorizontals)
                {
                    gridHorizontal.Y1 = gridHorizontal.Y2 = gridHorizontal.Y1 * coef;
                }
            }

            // grid verticals
            if (ChartScaler.elements.GridVerticals != null)
            {
                foreach (var gridVertical in ChartScaler.elements.GridVerticals)
                {
                    gridVertical.Y2 *= coef;
                }
            }

            // solid end of block lines
            if (ChartScaler.elements.SolidEndOfBlockLines != null)
            {
                foreach (var line in ChartScaler.elements.SolidEndOfBlockLines)
                {
                    line.Y1 = line.Y2 = line.Y1 * coef;
                }
            }

            // end of block lines
            if (ChartScaler.elements.EndOfBlockLines != null)
            {
                foreach (var line in ChartScaler.elements.EndOfBlockLines)
                {
                    line.Y1 = line.Y2 = line.Y1 * coef;
                }
            }
        }

        private static void ScaleCharts(double coef)
        {
            if (ChartScaler.elements.Charts != null)
            {
                foreach (var polyline in ChartScaler.elements.Charts)
                {
                    polyline.Height *= coef;
                    polyline.Margin = new Thickness(polyline.Margin.Left, polyline.Margin.Top * coef,
                        polyline.Margin.Right, polyline.Margin.Bottom * coef);
                }
            }
        }

        private static void ScaleDateTimeLabels(double coef)
        {
            if (ChartScaler.elements.TimeLabels != null)
            {
                foreach (var label in ChartScaler.elements.TimeLabels)
                {
                    label.Margin = new Thickness(
                        label.Margin.Left, label.Margin.Top * coef, label.Margin.Right, label.Margin.Bottom);
                }
            }

            if (ChartScaler.elements.DateLabels != null)
            {
                foreach (var label in ChartScaler.elements.DateLabels)
                {
                    label.Margin = new Thickness(
                        label.Margin.Left, label.Margin.Top * coef, label.Margin.Right, label.Margin.Bottom);
                }
            }
        }

        private static void ScaleRedLine(double coef)
        {
            if (ChartScaler.elements.RedLine != null 
                && ChartScaler.elements.CurrentDateLabel != null 
                && ChartScaler.elements.CurrentTimeLabel != null)
            {
                ChartScaler.elements.RedLine.Y1 = ChartScaler.elements.RedLine.Y2 *= coef;
                ChartScaler.elements.CurrentDateLabel.Margin = new Thickness(ChartScaler.elements.CurrentDateLabel.Margin.Left,
                    ChartScaler.elements.CurrentDateLabel.Margin.Top * coef, ChartScaler.elements.CurrentDateLabel.Margin.Right,
                    ChartScaler.elements.CurrentDateLabel.Margin.Bottom);

                ChartScaler.elements.CurrentTimeLabel.Margin = new Thickness(ChartScaler.elements.CurrentTimeLabel.Margin.Left,
                    ChartScaler.elements.CurrentTimeLabel.Margin.Top * coef, ChartScaler.elements.CurrentTimeLabel.Margin.Right,
                    ChartScaler.elements.CurrentTimeLabel.Margin.Bottom);
            }
        }
    }
}